package com.woniuxy.charin.listener;

import com.rabbitmq.client.Channel;
import com.woniuxy.charin.model.entity.Form;
import com.woniuxy.charin.service.FormService;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "form.queue")
public class FormListener {
    @Autowired
    private FormService formService;
    @SneakyThrows
    @RabbitHandler
    public void handle(Form form, Message message, Channel channel,
                       @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        formService.save(form);
        channel.basicAck(deliveryTag, false);
    }
}
